package o;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import com.huawei.appgallery.jsonkit.api.JsonBean;
import com.huawei.appmarket.service.activitydispatcher.OpenGateway.Param;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.lang.reflect.Field;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

public abstract class cft {
    protected static final Map<String, String> ˋ = new ds();

    static {
        ˋ.put(Param.TYPE_STR, "TEXT");
        ˋ.put(Param.TYPE_LONG, "INTEGER");
        ˋ.put(Param.TYPE_INT, "INTEGER");
        ˋ.put(Param.TYPE_FLOAT, "REAL");
        ˋ.put(Param.TYPE_BOOLEAN, "INTEGER");
        ˋ.put("JsonBean", "TEXT");
    }

    @NonNull
    public static String ˋ(String str, cfr o_cfr) {
        Field[] ˊ = dob.ˊ(o_cfr.getClass());
        StringBuilder stringBuilder = new StringBuilder(255);
        stringBuilder.append("insert into ");
        stringBuilder.append(str);
        StringBuilder stringBuilder2 = new StringBuilder(255);
        stringBuilder2.append(" (");
        StringBuilder stringBuilder3 = new StringBuilder(255);
        stringBuilder3.append(" (");
        for (int i = 0; i < ˊ.length; i++) {
            ˊ[i].setAccessible(true);
            String name = ˊ[i].getName();
            if (name.endsWith("_")) {
                stringBuilder2.append(ॱ(o_cfr, name)).append(",");
                stringBuilder3.append("?,");
            }
        }
        if (stringBuilder2.charAt(stringBuilder2.length() - 1) == ',') {
            stringBuilder2.deleteCharAt(stringBuilder2.length() - 1);
        }
        stringBuilder2.append(") ");
        if (stringBuilder3.charAt(stringBuilder3.length() - 1) == ',') {
            stringBuilder3.deleteCharAt(stringBuilder3.length() - 1);
        }
        stringBuilder3.append(") ");
        stringBuilder.append(stringBuilder2.toString());
        stringBuilder.append("values");
        stringBuilder.append(stringBuilder3.toString());
        return stringBuilder.toString();
    }

    private static String ˋ(cfr o_cfr, String str) {
        Field[] ˊ = dob.ˊ(o_cfr.getClass());
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("create table ");
        stringBuilder.append(str);
        stringBuilder.append(" ( ");
        stringBuilder.append("_id INTEGER primary key autoincrement ");
        int i = 0;
        while (i < ˊ.length) {
            ˊ[i].setAccessible(true);
            String name = ˊ[i].getName();
            if (name.endsWith("_")) {
                String str2 = (String) ˋ.get(ˊ[i].getType().getSimpleName());
                if (str2 == null && JsonBean.class.isAssignableFrom(ˊ[i].getType())) {
                    str2 = (String) ˋ.get("JsonBean");
                }
                if (str2 != null) {
                    name = ॱ(o_cfr, name);
                    stringBuilder.append(" , ");
                    stringBuilder.append(name);
                    stringBuilder.append(HwAccountConstants.BLANK);
                    stringBuilder.append(str2);
                }
            }
            i++;
        }
        stringBuilder.append(" ) ");
        return stringBuilder.toString();
    }

    @NonNull
    public static String ˎ(cfr o_cfr) {
        return ˋ(o_cfr, o_cfr.t_());
    }

    @NonNull
    public static ContentValues ॱ(cfr o_cfr) {
        Field[] ˊ = dob.ˊ(o_cfr.getClass());
        ContentValues contentValues = new ContentValues();
        for (int i = 0; i < ˊ.length; i++) {
            try {
                ˊ[i].setAccessible(true);
                String name = ˊ[i].getName();
                if (name.endsWith("_")) {
                    Object obj = ˊ[i].get(o_cfr);
                    if (obj != null) {
                        name = ॱ(o_cfr, name);
                        if (obj instanceof String) {
                            contentValues.put(name, (String) obj);
                        } else if (obj instanceof Integer) {
                            contentValues.put(name, (Integer) obj);
                        } else if (obj instanceof Long) {
                            contentValues.put(name, (Long) obj);
                        } else if (obj instanceof Boolean) {
                            contentValues.put(name, Integer.valueOf(((Boolean) obj).booleanValue() ? 1 : 0));
                        } else if (obj instanceof JsonBean) {
                            ˊ(name, contentValues, (JsonBean) obj);
                        } else {
                            Logger.e("DataSourceUtil", "unsupport type");
                        }
                    }
                }
            } catch (IllegalAccessException e) {
            }
        }
        return contentValues;
    }

    private static void ˊ(String str, ContentValues contentValues, JsonBean jsonBean) {
        String str2 = "";
        try {
            str2 = jsonBean.toJson();
        } catch (IllegalAccessException e) {
            Logger.e("DataSourceUtil", "can not toJson");
        }
        contentValues.put(str, str2);
    }

    private static String ॱ(cfr o_cfr, String str) {
        if (o_cfr.ˋॱ() && str.endsWith("_")) {
            return str.substring(0, str.length() - 1);
        }
        return str;
    }

    public static void ˋ(cfr o_cfr, Cursor cursor) {
        Field[] ˊ = dob.ˊ(o_cfr.getClass());
        for (int i = 0; i < ˊ.length; i++) {
            try {
                ˊ[i].setAccessible(true);
                String name = ˊ[i].getName();
                if (name.endsWith("_")) {
                    String simpleName = ˊ[i].getType().getSimpleName();
                    int columnIndex = cursor.getColumnIndex(ॱ(o_cfr, name));
                    if (columnIndex != -1) {
                        if (simpleName.equals(Param.TYPE_STR)) {
                            ˊ[i].set(o_cfr, cursor.getString(columnIndex));
                        } else if (simpleName.equals(Param.TYPE_INT)) {
                            ˊ[i].set(o_cfr, Integer.valueOf(cursor.getInt(columnIndex)));
                        } else if (simpleName.equals(Param.TYPE_LONG)) {
                            ˊ[i].set(o_cfr, Long.valueOf(cursor.getLong(columnIndex)));
                        } else if (simpleName.equals(Param.TYPE_FLOAT)) {
                            ˊ[i].set(o_cfr, Float.valueOf(cursor.getFloat(columnIndex)));
                        } else if (simpleName.equals(Param.TYPE_BOOLEAN)) {
                            ˊ[i].set(o_cfr, Boolean.valueOf(cursor.getInt(columnIndex) == 1));
                        } else if (JsonBean.class.isAssignableFrom(ˊ[i].getType())) {
                            ˎ(o_cfr, cursor, ˊ, columnIndex, i);
                        } else {
                            Logger.e("DataSourceUtil", "unsupport field type:" + simpleName + HwAccountConstants.BLANK + ˊ[i].getName());
                        }
                    }
                }
            } catch (Throwable e) {
                Logger.ˋ("DataSourceUtil", "IllegalAccessException:", e);
            }
        }
    }

    private static void ˎ(cfr o_cfr, Cursor cursor, Field[] fieldArr, int i, int i2) throws IllegalAccessException {
        try {
            Object string = cursor.getString(i);
            if (!TextUtils.isEmpty(string)) {
                JSONObject jSONObject = new JSONObject(string);
                JsonBean jsonBean = (JsonBean) fieldArr[i2].getType().newInstance();
                jsonBean.fromJson(jSONObject);
                fieldArr[i2].set(o_cfr, jsonBean);
            }
        } catch (JSONException e) {
            Logger.e("DataSourceUtil", "can not paser json");
        } catch (InstantiationException e2) {
            Logger.e("DataSourceUtil", "can not instance ");
        } catch (ClassNotFoundException e3) {
            Logger.e("DataSourceUtil", "can not find class");
        }
    }

    public static void ˊ(cfr o_cfr, SQLiteStatement sQLiteStatement) {
        Field[] ˊ = dob.ˊ(o_cfr.getClass());
        int i = 1;
        for (int i2 = 0; i2 < ˊ.length; i2++) {
            try {
                ˊ[i2].setAccessible(true);
                if (ˊ[i2].getName().endsWith("_")) {
                    Object obj = ˊ[i2].get(o_cfr);
                    if (obj == null) {
                        int i3 = i + 1;
                        sQLiteStatement.bindNull(i);
                        i = i3;
                    } else if (obj instanceof String) {
                        r3 = i + 1;
                        sQLiteStatement.bindString(i, (String) obj);
                        i = r3;
                    } else if (obj instanceof Integer) {
                        r3 = i + 1;
                        sQLiteStatement.bindLong(i, (long) ((Integer) obj).intValue());
                        i = r3;
                    } else if (obj instanceof Long) {
                        r3 = i + 1;
                        sQLiteStatement.bindLong(i, ((Long) obj).longValue());
                        i = r3;
                    } else if (obj instanceof Boolean) {
                        r3 = i + 1;
                        sQLiteStatement.bindLong(i, ((Boolean) obj).booleanValue() ? 1 : 0);
                        i = r3;
                    } else if (obj instanceof JsonBean) {
                        i = ˎ(sQLiteStatement, i, (JsonBean) obj);
                    } else {
                        Logger.e("DataSourceUtil", "unsupport type");
                    }
                }
            } catch (IllegalAccessException e) {
            }
        }
    }

    private static int ˎ(SQLiteStatement sQLiteStatement, int i, JsonBean jsonBean) {
        int i2 = i + 1;
        try {
            sQLiteStatement.bindString(i, jsonBean.toJson());
        } catch (Throwable e) {
            Logger.ˋ("DataSourceUtil", "toJson failed", e);
        }
        return i2;
    }
}
